package org.skydiveapplibrary.ui;

import org.skydiveapplibrary.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

public class AuthenticationDialog extends AlertDialog
{
	// listener definition
	public interface AuthenticationListener
	{
		void authenticate(String userName, String password);
	}
	
	// listeners
	private AuthenticationListener dialogListener;
	private OnClickListener okClickListener = new OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			handleOKClick();
		}
	};
	private OnClickListener cancelClickListener = new OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			dismiss();
		}
	};
	
	public AuthenticationDialog(
			Context context,
			AuthenticationListener dialogListener)
	{
		super(context);
		
		this.dialogListener = dialogListener;
		
		// get view
		View view = View.inflate(context, R.layout.authentication_dialog, null);
		
		// init view
		setView(view, 10, 10, 10, 10);
		setButton(
				context.getString(android.R.string.ok),
				okClickListener);
		setButton2(
				context.getString(android.R.string.cancel),
				cancelClickListener);
		setTitle(context.getString(R.string.authentication_title));
	}
	
	private void handleOKClick()
	{
		// get username/password
		TextView userNameField = (TextView)findViewById(R.id.UserNameField);
		String userName = userNameField.getText() == null ? null : userNameField.getText().toString();
		
		TextView passwordField = (TextView)findViewById(R.id.PasswordField);
		String password = passwordField.getText() == null ? null : passwordField.getText().toString();
		
		// do authentication
		dialogListener.authenticate(userName, password);
	}
}
